<template>
  <div>
<!-- sync控制 -->
  <el-dialog
  title="修改"
  :visible.sync="dialogVisible"
  @update:visible="setDialogVisible(false)"
  width="30%"
  >
  <el-form :model="student" status-icon :rules="rules" ref="student" label-width="100px" class="demo-ruleForm">
 <el-form-item label="姓名" prop="name">
    <el-input v-model="student.name"></el-input>
  </el-form-item>
  <!-- <el-form-item label="年龄" prop="age">
    <el-input  v-model.number="student.age" ></el-input>
  </el-form-item> -->
  <el-form-item label="性别" >
  <el-radio-group v-model="student.gender">
    <el-radio label="男">男</el-radio>
    <el-radio label="女">女</el-radio>
  </el-radio-group>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('student')">提交</el-button>
    <el-button @click="setDialogVisible(false)">取消</el-button>
  </el-form-item>
  </el-form>
  
</el-dialog>
</div>
</template>

<script>
import {update} from '../service'
import {mapState} from 'vuex'
import {mapMutations,mapActions} from 'vuex'
export default {
  computed:{
     ...mapState("student",["dialogVisible","student"]),//...拓展运算符，将mapState展开当作一个键，‘count’为值
      
    },
    // props:["dialogVisible","student"],
    data(){
       return {
        ruleForm: {
          name: '',
          gender: ''
        },
        rules: {
          name: [
            { required:true, trigger: 'blur' }
          ],
          gender: [
            { required:true, trigger: 'blur' }
          ],
        }
   }
  },
  methods: {
      //this.$refs获取元素的，页面组件的方法
      submitForm(formName) {
          // console.log(formName);
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
              await update(this.student);
            this.setDialogVisible(false);
            this.setStudent({});
            this.setStudents({});
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      ...mapMutations("student",["setDialogVisible","setStudent"]),
      ...mapActions("student",["setStudents"])
        
    },
    

}
</script>

<style>

</style>